﻿using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;

namespace UniBrickServer.Utils
{
    public class PlayerUtils
    {
        public static bool UserExists(string username)
        {
            var sql = String.Format("SELECT EXISTS (SELECT 1 FROM Accounts WHERE Username = '{0}');", username);
            var command = new MySqlCommand(sql, DatabaseUtils.Connection);
            var reader = command.ExecuteReader();
            reader.Read();
            var exists = reader.GetBoolean(0);
            reader.Close();

            return exists;
        }

        public static bool IsUserBanned(string username)
        {
            // TODO: Check if User is in banlist file

            return false;
        }

        public static bool IsIpBanned(string ipAddress)
        {
            // TODO: Check if IP is in banlist file

            return false;
        }

        public static void CreateUser(string username, string password, Dictionary<byte, object> data)
        {
            password = EncryptionUtils.Hash(password);

            var userData = JsonConvert.SerializeObject(data);
            var sql = String.Format(
                "INSERT INTO Accounts (Username, Password, UserData) VALUES ('{0}', '{1}', '{2}');", username, password,
                userData);
            var command = new MySqlCommand(sql, DatabaseUtils.Connection);
            command.ExecuteNonQuery();
        }

        public static void DeleteUser(string username)
        {
            var sql = String.Format("DELETE FROM Accounts WHERE Username='{0}';", username);
            var command = new MySqlCommand(sql, DatabaseUtils.Connection);
            command.ExecuteNonQuery();
        }

        public static string GetPassword(string username)
        {
            var sql = String.Format("SELECT Password FROM Accounts WHERE Username = '{0}';", username);
            var command = new MySqlCommand(sql, DatabaseUtils.Connection);
            var password = (string) command.ExecuteScalar();
            return password;
        }

        public static Dictionary<byte, object> LoadPlayerData(string username)
        {
            var sql = String.Format("SELECT UserData FROM Accounts WHERE Username = '{0}';", username);
            var command = new MySqlCommand(sql, DatabaseUtils.Connection);
            var jsonData = (string) command.ExecuteScalar();
            var data =
                JsonConvert.DeserializeObject<Dictionary<byte, object>>(jsonData);
            return data;
        }

        public static void SavePlayerData(string username, Dictionary<byte, object> data)
        {
            var userData = JsonConvert.SerializeObject(data);
            var sql = String.Format("UPDATE Accounts SET UserData='{0}' WHERE Username='{1}';", userData, username);
            var command = new MySqlCommand(sql, DatabaseUtils.Connection);
            command.ExecuteNonQuery();
        }
    }
}